﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;

namespace AnyOffice.general.vehicle.apply
{
    public partial class _new : ModulePage
    {

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                VehicleDao dao = new VehicleDao();
                DataTable table = dao.SelectAll(base.Conn);
                this.ddlVehicleId.DataSource = table;
                this.ddlVehicleId.DataValueField = "VehicleId";
                this.ddlVehicleId.DataTextField = "Number";
                this.ddlVehicleId.DataBind();
                DataTable table2 = dao.SelectOperator(base.Conn);
                this.ddlOperator.DataSource = table2;
                this.ddlOperator.DataValueField = "UserId";
                this.ddlOperator.DataTextField = "UserName";
                this.ddlOperator.DataBind();
                UserCache cache = new UserCache();
                this.txtProposer.Text = cache.UserName;
                this.txtStartTime.Text = DateTime.Now.ToString();
                this.txtEndTime.Text = DateTime.Now.ToString();
                this.ddlVehicleId.Attributes.Add("onchange", "showDetail();");
                this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.Vehicle);
                this.ckbMobileRemind.Visible = SmsDao.IsMobile(SmsDao.SmsType.Vehicle);
            }
            if (this.Page.IsPostBack)
            {
                VehicleUsageDao dao2 = new VehicleUsageDao();
                VehicleUsageData data = new VehicleUsageData();
                data.VehicleId = Convert.ToInt32(this.ddlVehicleId.SelectedValue);
                if (this.txtDriver.Text != string.Empty)
                {
                    data.Driver = this.txtDriver.Text;
                }
                if (this.txtPerson.Text != string.Empty)
                {
                    data.Person = this.txtPerson.Text;
                }
                if (this.ToId.Value != string.Empty)
                {
                    data.DeptId = Convert.ToInt32(this.ToId.Value);
                }
                data.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
                data.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                if (this.txtDestination.Text != string.Empty)
                {
                    data.Destination = this.txtDestination.Text;
                }
                if (this.txtMileage.Text != string.Empty)
                {
                    data.Mileage = Convert.ToDecimal(this.txtMileage.Text);
                }
                data.Proposer = base.User.Identity.Name;
                data.Operator = this.ddlOperator.SelectedValue;
                if (this.txtReason.Text != string.Empty)
                {
                    data.Reason = this.txtReason.Text;
                }
                if (this.txtRemark.Text != string.Empty)
                {
                    data.Remark = this.txtRemark.Text;
                }
                data.SmsRemind = this.ckbSmsRemind.Checked;
                data.MobileRemind = this.ckbMobileRemind.Checked;
                dao2.Insert(base.Conn, data);
                if ((this.ckbSms.Checked || this.ckbMobile.Checked) && (this.ddlOperator.SelectedValue != string.Empty))
                {
                    SmsDao dao3 = new SmsDao();
                    SmsData data2 = new SmsData();
                    data2.Type = 9;
                    data2.FromId = base.User.Identity.Name;
                    data2.ToId = this.ddlOperator.SelectedValue;
                    data2.Content = "您有新的车辆申请需要审批。";
                    data2.SendTime = DateTime.Now;
                    dao3.BizSend(base.Conn, data2, this.ckbSms.Checked, this.ckbMobile.Checked);
                }
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowPrompt("车辆申请添加成功！", "new.aspx");
            }
        } 

    }
}
